// JavaScript Document
function showEditArea(val)
{
	if(val == 'resource')
	{
		$("#resource").show();
		$("#intro").hide();
		$("#editRes").attr("class",'current');
		$("#editIntro").attr('class','');
	}
	else
	{
		$("#resource").hide();
		$("#intro").show();
		$("#editIntro").attr('class','current');
		$("#editRes").attr("class",'');
	}
	
}

function mulDelete()
{
	var form = document.getElementById('mul');
	if(!confirm("您确认要删除这些文件吗？"))
	{
		return false;
	}
	form.action="upload.php?action=muldelete";
	form.submit();
}

function mulRename()
{
	var form = document.getElementById('mul');
	form.action="upload.php?action=rename";
}

ifcheck = true;
function CheckAll(form)
{
	for (var i=0;i<form.elements.length-2;i++)
	{
		var e = form.elements[i];
		if(e.type=='checkbox') e.checked = ifcheck;
	}
	ifcheck = ifcheck == true ? false : true;
}

var selectCheck = 0;
function whole(form)
{
	for (var i=0;i<form.elements.length-2;i++)
	{
		var e = form.elements[i];
		if(e.type=='checkbox'){
			if(e.checked==true) selectCheck++;
		}
	}
	if(selectCheck<=0){
		alert("请选择操作对象");
		return false;
	}
}


function saveTitle(rid)
{
	var title = document.getElementById('filetitle_'+rid).value;
	var url = 'upload.php?action=edittitle&rid=' + rid + '&title=' + title + '&tid=' + tid;
	var xhr = new XHR("saveTitleOK");
	//alert(title);
	xhr.post(url);
	return false;
}

function saveTitleOK(res)
{
	if(res == '100')
	{
		alert("文件标题修改成功");
	}
	else
	{
		alert(res);
	}
}

function objectSWF(tid)
{
	//photo_swfu = new PhotoUpload("script/swfupload_f9.swf","$very[url]/upload.php?tid=" + tid);
}

var removeRid = '';
function removeRes(rid)
{
	var con = confirm("您确认要从本内容中移除此资源么？");
	if(!con) return false;
	var xhr = new XHR('removeRes2');
	var url = 'upload.php?action=removeRes&rid='+rid + '&tid=' + tid;
	//alert(url);
	xhr.get(url);
	removeRid = rid;
}

function removeRes2(msg)
{
	//alert(msg);
	if(msg == 1)
	{
		var inputTable = document.getElementById('resourceTbody');
		var resTr = document.getElementById('res_'+removeRid);
		inputTable.removeChild(resTr);
	}
}


function checkRes()
{
	if(document.getElementById('mulu').value == '')
	{
		alert('请选择一个有效目录');
		return false;
	}
	if(document.getElementById('title').value.length < 2)
	{
		alert('内容标题为空或者长度小于2，请务必填写此项。');
		document.getElementById('title').focus();
		return false;
	}
	$('#upbtn').attr("disable", "disabled");

}

function IsBrowser(){
	var sAgent = navigator.userAgent.toLowerCase() ;
	// Internet Explorer
	if ( sAgent.indexOf("msie") != -1 && sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1 )
		return "msie" ;

	// Gecko (Opera 9 tries to behave like Gecko at this point).
	if ( navigator.product == "Gecko" && !( typeof(opera) == 'object' && opera.postError ) )
		return "gecko";

	// Opera
	if ( navigator.appName == 'Opera')
		return "opera" ;

	// Safari
	if ( sAgent.indexOf( 'safari' ) != -1 )
		return "safari";

	return false ;
}

function selectImg(inputname){
	if(IsBrowser()=='msie'){
		showModalDialog(basename + "&action=addimage&inputtype=input&inputname="+inputname,window,'dialogWidth=650px;dialogHeight=500px;help:no;status:no;');
	}else{
		window.open(basename + "&action=addimage&inputtype=input&inputname="+inputname,'','width=650,height=500,status=no');
	}
}
var tagid = document.FORM.tagsid;
var tagname;
var tags;
function SelectTag(tag,id){
	tagname = tag;
	var newid = parseInt(id);
	if(tagid.value.indexOf(newid)=='-1'){
		tagid.value+=newid+',';
		document.getElementById('tags_show').innerHTML+="<A href=\"javascript:deltag("+newid+",'"+tagname+"');\">"+tagname+"</A>   ";
	}
}

function SearchTag(evt){
	e=catchKeyCode(evt);
	if(e==13){
		addtag();
	}else{
		tagname = document.FORM.tags.value;
		tagname=tagname.replace(/(^\s*)|(\s*$)/g, "").replace(/\s+/g," ");
		tags = tagname.split(" ");
		if(tags[tags.length-1].length>0){
			var xhr = new XHR('ShowSearchTag');
			var url = basename + '&action=SearchTag&tagname='+encodeURI(tags[tags.length-1]);
			xhr.get(url);
		}else{
			document.getElementById('showtags').style.display ='none';
		}
	}
}

function ShowSearchTag(ret){
	var st = document.getElementById('showtags');
	if(ret!='error'){
		st.innerHTML=ret;
		st.style.display ='block';
	}else{
		st.innerHTML='';
		st.style.display ='none';
	}
}

function catchKeyCode(evt){
	evt=window.event||evt;
	var ret=evt.keyCode?evt.keyCode:evt.which;
	return ret;
}

function addtag(){
	var j=0;
	tagname = document.FORM.tags.value;
	tagname=tagname.replace(/(^\s*)|(\s*$)/g, "").replace(/\s+/g," ");
	tags = tagname.split(" ");
	for(i in tags){
		if(tags[i].length<=1){
			alert('每一个标签(tag)必须由两个以上的字符组成');
			return false;
		}
	}
	var xhr = new XHR('showtag');
	var url = basename + '&action=tags&tagname='+encodeURI(tagname);
	xhr.get(url);
}

function showtag(ret){
	var newid = ret.split(',');
	for(i in tags){
		if(tagid.value.indexOf(newid[i]+',')=='-1'){
			tagid.value+=newid[i]+',';
			var browser = IsBrowser();
			document.getElementById('tags_show').innerHTML+="<A href=\"javascript:deltag("+newid[i]+",'"+tags[i]+"');\">"+tags[i]+"</A>   ";
			document.FORM.tags.value='';
		}
	}
}

function deltag(id,tagname){
	var ss=id+',';
	var nn;
	var browser = IsBrowser();
	if(browser=='msie' || browser=='opera'){
		nn="<A href=\"javascript:deltag("+id+",'"+tagname+"');\">"+tagname+"</A>";
	}else{
		nn="<a href=\"javascript:deltag("+id+",'"+tagname+"');\">"+tagname+"</a>";
	}
	var valueNow = tagid.value.replace(ss,' ');
	var tagNow = document.getElementById('tags_show').innerHTML.replace(nn,' ');
	document.getElementById('tags_show').innerHTML = tagNow;
	tagid.value=valueNow;
	document.FORM.tags.value=tagname;
}

function selectValue(inputname,value){
	if(value!=''){
		document.getElementById(inputname).value=value;
	}
}

function showColor(showName,inputName){
	var menu_editor = getObj("menu_editor");
	var colors = [
		'000000','660000','663300','666600','669900','66CC00','66FF00','666666','660066','663366','666666',
		'669966','66CC66','66FF66','CCCCCC','6600CC','6633CC','6666CC','6699CC','66CCCC','66FFCC','FF0000',
		'FF0000','FF3300','FF6600','FF9900','FFCC00','FFFF00','0000FF','FF0066','FF3366','FF6666','FF9966',
		'FFCC66','FFFF66','00FFFF','FF00CC','FF33CC','FF66CC','FF99CC','FFCCCC','FFFFCC'
	];
	var html = '<div id="colorbox">';
	for(i in colors){
		html += "<div unselectable=\"on\" style=\"background:#" + colors[i] + "\" onClick=\"SetC('" + colors[i] + "','" + inputName + "')\"></div>";
	}
	html += '</div>';
	menu_editor.innerHTML = html;
	if(typeof type == 'undefined'){
		click_open('menu_editor',showName,'2');
	} else{
		mouseover_open('menu_editor',showName,'2');
	}
}
function SetC(color,inputName){
	//var textValue = document.getElementById(inputName).value;
	//document.getElementById(inputName).value = "<span style=\"color:#"+color+";\">"+textValue+"</span>";
	document.getElementById(inputName).value = "#"+color;
	closep();
}
